/*
 * MemoRecord.java
 *
 * Created on 2008年5月26日, 上午11:43
 */
package calendar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

/**
 *
 * @author  啊正
 */
public class MemoRecord extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	private String systemMemo = "";
    private String date = "";
    private String path = "d:/Calendar_Memo/Memo.BOX";
    private String memo = "";
    private String today = "";
    BufferedReader br = null;
    BufferedWriter bw = null;
    String readFile = new String();
    String writeFile = new String();
    private static String operateMemo = "alter";

    /** Creates new form MemoRecord */
    public MemoRecord(String date, String today) {
        initComponents();
        this.date = date;
        if (date.equals("allMemo")) {
            spTab.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 204),
                    1, true), "所有待办事件", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        }
        this.today = today;
        updateTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPane = new javax.swing.JPanel();
        spTab = new javax.swing.JScrollPane();
        tabMemo = new javax.swing.JTable();
        panInf = new javax.swing.JPanel();
        labDate = new javax.swing.JLabel();
        txtDate = new javax.swing.JTextField();
        spTa = new javax.swing.JScrollPane();
        taMemo = new javax.swing.JTextArea();
        labStatus = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        btnAlterMemo = new javax.swing.JButton();
        btnDelMemo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("查看备忘录");
        setModal(true);
        setResizable(false);

        contentPane.setBackground(java.awt.SystemColor.inactiveCaptionText);

        spTab.setBackground(new java.awt.Color(204, 255, 204));
        spTab.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 204), 1, true), "今天待办事件", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        tabMemo.setAutoCreateRowSorter(true);
        tabMemo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "事件"
            }
        ) {
            /**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabMemo.getTableHeader().setReorderingAllowed(false);
        tabMemo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabMemoMouseClicked(evt);
            }
        });
        spTab.setViewportView(tabMemo);

        panInf.setBackground(new java.awt.Color(204, 204, 255));
        panInf.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        labDate.setText("日期：");

        txtDate.setBackground(new java.awt.Color(255, 255, 255));
        txtDate.setEditable(false);

        spTa.setBackground(new java.awt.Color(255, 255, 255));
        spTa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 204), 1, true), "事件内容", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 12), new java.awt.Color(0, 0, 0)));

        taMemo.setLineWrap(true);
        spTa.setViewportView(taMemo);

        labStatus.setText("状态：");

        txtStatus.setBackground(new java.awt.Color(255, 255, 255));
        txtStatus.setEditable(false);

        btnAlterMemo.setText("修改");
        btnAlterMemo.setEnabled(false);
        btnAlterMemo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterMemoActionPerformed(evt);
            }
        });

        btnDelMemo.setText("删除");
        btnDelMemo.setEnabled(false);
        btnDelMemo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelMemoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panInfLayout = new javax.swing.GroupLayout(panInf);
        panInf.setLayout(panInfLayout);
        panInfLayout.setHorizontalGroup(
            panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panInfLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panInfLayout.createSequentialGroup()
                        .addComponent(labDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDate, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(spTa, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                .addGroup(panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panInfLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(labStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panInfLayout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAlterMemo)
                            .addComponent(btnDelMemo))))
                .addContainerGap())
        );
        panInfLayout.setVerticalGroup(
            panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panInfLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labDate)
                    .addComponent(txtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labStatus)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panInfLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panInfLayout.createSequentialGroup()
                        .addComponent(btnAlterMemo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelMemo)
                        .addGap(7, 7, 7))
                    .addComponent(spTa, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panInf, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(spTab, 0, 0, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addComponent(spTab, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panInf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(contentPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contentPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-348)/2, (screenSize.height-269)/2, 348, 269);
    }// </editor-fold>//GEN-END:initComponents
    private void tabMemoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabMemoMouseClicked
        // TODO add your handling code here:
        btnAlterMemo.setEnabled(true);
        btnDelMemo.setEnabled(true);
        if (!date.equals("allMemo")) {
            txtDate.setText(date);
            taMemo.setText(tabMemo.getValueAt(tabMemo.getSelectedRow(), tabMemo.getSelectedColumn()).toString());
        } else {
            txtDate.setText(tabMemo.getValueAt(tabMemo.getSelectedRow(), 0).toString());
            taMemo.setText(tabMemo.getValueAt(tabMemo.getSelectedRow(), 1).toString());
        }
        String temp = txtDate.getText();
        String[] minmax = temp.split("-");
        String day = minmax[0] + minmax[1] + minmax[2];
        String[] tempToday=today.split("-");
        String nowDate=tempToday[0]+tempToday[1]+tempToday[2];
        if(Integer.parseInt(day)<Integer.parseInt(nowDate)){
            txtStatus.setText("已过时");
        }else{
            txtStatus.setText("未过时");
        }
        memo = taMemo.getText().trim();
    }//GEN-LAST:event_tabMemoMouseClicked

    private void btnAlterMemoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterMemoActionPerformed
        // TODO add your handling code here:
        operateMemo = "alter";
        operateMemo();
}//GEN-LAST:event_btnAlterMemoActionPerformed

    private void btnDelMemoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelMemoActionPerformed
        // TODO add your handling code here:
        operateMemo = "del";
        operateMemo();
    }//GEN-LAST:event_btnDelMemoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterMemo;
    private javax.swing.JButton btnDelMemo;
    private javax.swing.JPanel contentPane;
    private javax.swing.JLabel labDate;
    private javax.swing.JLabel labStatus;
    private javax.swing.JPanel panInf;
    private javax.swing.JScrollPane spTa;
    private javax.swing.JScrollPane spTab;
    private javax.swing.JTextArea taMemo;
    private javax.swing.JTable tabMemo;
    private javax.swing.JTextField txtDate;
    private javax.swing.JTextField txtStatus;
    // End of variables declaration//GEN-END:variables
    private void updateTable() {
        Object[][] obj = null;
        String[] str = null;
        String[] columns = null;
        if (!date.equals("allMemo")) {
            systemMemo = new CheckMemo().getTadayMemo();
            str = systemMemo.split("_");
            obj = new Object[str.length][1];
            for (int i = 0; i < str.length; i++) {
                String[] strMemo = str[i].split(",");
                obj[i][0] = strMemo[1];
            }
            columns = new String[]{"事件"};
        } else {
            systemMemo = new CheckMemo().getAllMemo();
            str = systemMemo.split("_");
            obj = new Object[str.length][2];
            for (int i = 0; i < str.length; i++) {
                String[] strMemo = str[i].split(",");
                obj[i][0] = strMemo[0];
                obj[i][1] = strMemo[1];
            }
            columns = new String[]{"时间", "事件"};
        }

        if (tabMemo == null) {
            tabMemo = new javax.swing.JTable();
        }
        tabMemo.removeAll();
        tabMemo.setModel(new javax.swing.table.DefaultTableModel(obj, columns) {

			private static final long serialVersionUID = 1L;

			@Override
            public boolean isCellEditable(int rows, int columns) {
                return false;
            }
            });
    }

    //此方法相关注释请查看类WriteMemo
    private void operateMemo() {
        String day = txtDate.getText().trim();
        try {
            File file = new File(path);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while ((readFile = br.readLine()) != null) {
                String[] str = readFile.split(",");
                if (str[0].equals(day) && str[1].equals(memo)) {
                    if (operateMemo.equals("alter")) {
                        writeFile += day + "," + taMemo.getText().trim() + "\r\n";
                    } else {
                        writeFile += "";
                    }
                } else {
                    writeFile += readFile + "\r\n";
                }
            }
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(writeFile);
        } catch (Exception ex) {
        } finally {
            try {
                br.close();
                bw.close();
                JOptionPane.showMessageDialog(this, "操作成功!");
                updateTable();
            } catch (IOException ex) {
            }
        }
    }
}
